//
//  CustomerViewController.swift
//  Swift训练
//
//  Created by zhenjingxiong on 2018/8/30.
//  Copyright © 2018年 zhenjingxiong. All rights reserved.
//

import UIKit

class CustomerViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.layoutUI()
    }
    
    
    lazy var listView: UITableView = {
        let listView = UITableView.init()
        listView.delegate = self as UITableViewDelegate
        listView.dataSource = self as UITableViewDataSource
        listView.tableFooterView = UIView.init()
        return listView
    }()
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomerIdentification")
        cell?.textLabel?.text = "左边标题"
        cell?.detailTextLabel?.text = "右边标题"
        return cell!
    }
    
    //MARK:创建UI
    lazy var headView: CustomerHeadView = {
        let headView = CustomerHeadView.init(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth, height: 100))
        headView.backgroundColor = UIColor.darkGray
        return headView
    }()
    
    func layoutUI() {
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(clickRight))
        
        self.view.addSubview(self.listView)
        
        self.listView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        self.listView.register(UITableViewCell.self, forCellReuseIdentifier: "CustomerIdentification")
        self.listView.tableHeaderView = self.headView

        let imageArray = ["client_xinzeng_c","client_guanzhu_c","icon_group","client_qiangke_c","client_daorudaochu_c"]
        let titleArray = ["新增","关注客户","客户分组","抢客","导入导出"]
        
        
        for i in 0...4 {
            
            let subView = UIView.init(frame: CGRect.init(x:ScreenWidth/5 * CGFloat(i), y: 0, width: ScreenWidth/5, height: 80))
           subView.backgroundColor = UIColor.green
            subView.tag = 1000 + i
            self.headView.addSubview(subView)
            
            //添加一个手势/Users/zhenjingxiong/Desktop/学习Swift/SwiftOfXiaoShouJia/SwiftOfXiaoShouJia/Home/View/extension.swift
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapClick))
            subView.addGestureRecognizer(tap)
            print("%@",tap)
            
            //添加图标
            let subImageView = UIImageView.init(image: UIImage.init(named: imageArray[i]))
            subView.addSubview(subImageView)
            subImageView.snp.makeConstraints { (make) in
                make.center.equalTo(subView)
            }
            //添加文字
            let textLabel = UILabel.init()
            textLabel.text = titleArray[i]
            textLabel.font = UIFont.systemFont(ofSize: 10)
            subView.addSubview(textLabel)
            textLabel.snp.makeConstraints { (make) in
                make.centerX.equalTo(subView.snp.centerX)
                make.top.equalTo(subImageView.snp.bottom).offset(5)
            }
            
        }
 
    }
    
    @objc func tapClick(sender:UITapGestureRecognizer) {
        print("打印一下%@",sender)
        let subView = sender.view
        
        switch ((subView?.tag)! - 1000) {
        case 0:
           print("新增客户")
        case 1:
           print("关注客户")
        case 2:
           print("客户分组")
        case 3:
           print("抢客")
        case 4:
           print("导入导出")
        default:
            break
        }
        
    }
    
    @objc func clickRight() {
        print("祝你一路顺风")
        let SC = SearchCustomerViewController()
        self.navigationController?.pushViewController(SC, animated: true)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
